package com.ccnode.codegenerator.taikaoback.security.handler;

import com.ccnode.codegenerator.taikaoback.util.R;
import com.alibaba.fastjson.JSON;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 授权失败处理器
 */
@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
    
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        // 设置响应状态码
        response.setStatus(200);
        // 设置响应头
        response.setContentType("application/json;charset=UTF-8");
        // 设置响应内容
        response.getWriter().write(JSON.toJSONString(R.error(403, "权限不足，访问被拒绝")));
    }
}